const { ccclass, property } = cc._decorator;

@ccclass
export default class ResMgr extends cc.Component {
    private totalAb: number = 0;
    private nowAb: number = 0;

    private total: number = 0;
    private now: number = 0;

    private abBunds: any = {};
    private progressFunc: Function | null = null;
    private endFunc: Function | null = null;

    public static Instance: ResMgr = null as unknown as ResMgr;

    protected onLoad(): void {
        console.log(Date.now());
        if (ResMgr.Instance === null) {
            ResMgr.Instance = this;
        } else {
            this.destroy();
            return;
        }
    }

    /**
     *
     * @param abName  bundle包的名称
     * @param endFun  加载完成后的回调函数
     */
    private loadAssetsBundle(abName: string, endFun: Function): void {
        cc.assetManager.loadBundle(abName, (err, bundle) => {
            if (err !== null) {
                cc.log('[ResMgr]:Load AssetsBundle Error:' + abName);
                this.abBunds[abName] = null;
            } else {
                cc.log('[ResMgr]:Load AssetsBundle Success:' + abName);
                this.abBunds[abName] = bundle;
            }
            endFun && endFun();
        });
    }

    private loadRes(abBunds: any, url: any, typeClass: any): void {
        abBunds.load(url, typeClass, (error: any, asset: any) => {
            this.now++;
            if (error) {
                console.log('load Res ' + url + 'error: ' + error);
            } else {
                console.log('load Res' + url + ' successs!');
            }

            this.progressFunc && this.progressFunc(this.now, this.total);
            console.log(this.now, this.total);
            if (this.now >= this.total) {
                this.endFunc && this.endFunc();
            }
        });
    }

    private loadAssetsInAssetsBundle(resPkg: any): void {
        for (let key in resPkg) {
            const urlSet = resPkg[key].urls;
            const typeClass = resPkg[key].assetType;
            for (let i = 0, len = urlSet.length; i < len; i++) {
                this.loadRes(this.abBunds[key], urlSet[i], typeClass);
            }
        }
    }

    // 预加载资源包
    public preloadResPkg(resPkg: any, progressFun: Function | null, endFun: Function) {
        // 重置资源数量信息
        this.total = 0;
        this.now = 0;
        this.totalAb = 0;
        this.nowAb = 0;
        //进度显示函数
        this.progressFunc = progressFun;
        //资源加载完成后的操作
        this.endFunc = endFun;

        for (let key in resPkg) {
            this.totalAb++;
            this.total += resPkg[key].urls.length;
        }

        for (let key in resPkg) {
            this.loadAssetsBundle(key, () => {
                this.nowAb++;
                if (this.nowAb === this.totalAb) {
                    this.loadAssetsInAssetsBundle(resPkg);
                }
            });
        }
    }
    // 卸载资源包
    public unloadResPkg(resPkg: any) {}
    // 预加载资源
    public preloadRes(abName: string, url: string, progressFun: Function, endFun: Function) {}

    // 获取指定资源
    public getAsset(abName: string, resUrl: string): any {
        const bundle = cc.assetManager.getBundle(abName);
        if (bundle === null) {
            console.log('[error]: ' + abName + 'AssetBundle not loaded!!!');
            return null;
        }
        console.log(this.abBunds, 'this.abBunds');
        return bundle.get(resUrl);
    }
}
